<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/all.min.css">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/all.min.js"></script>
    <style>
        body {
            background: url("img/pipes.png") repeat;
        }

        .Icon {
            width: 40px;
            font-size: 26px;
            color: Dodgerblue;
            text-align: center;
        }
    </style>
    <script>
        let username, email, password1, password2;
        $(function () {
            $("#in1").blur(function () {
                username = false;
                const inp = $(this).val();
                const rul = /^[0-9a-zA-Z_]{3,10}$/;
                if (inp === "") {
                    $("#sp1").html("用户名不能为空");
                }
                else if (inp.length < 3 || inp.length > 10) {
                    $("#sp1").html("用户名长度为3~10位");
                }
                else if (!rul.test(inp)) {
                    $("#sp1").html("格式错误，应仅由数字、字母和下划线");
                }
                else {
                    $("#sp1").html("");
                    username = true;
                }
            });
            $("#in2").blur(function () {
                email = false;
                const inp = $(this).val();
                const rul = /^[a-zA-Z0-9]{1,10}@[a-zA-Z0-9]{1,5}\.[a-zA-Z0-9]{1,5}$/;
                if (inp === "") {
                    $("#sp2").html("电子邮箱不能为空");
                }
                else if (!rul.test(inp)) {
                    $("#sp2").html("格式错误");
                }
                else {
                    $("#sp2").html("");
                    email = true;
                }
            });
            $("#in3").blur(function () {
                password1 = false;
                const inp = $(this).val();
                const rul = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}$/;
                if (inp === "") {
                    $("#sp3").html("密码不能为空");
                }
                else if (inp.length < 8 || inp.length > 16) {
                    $("#sp3").html("密码长度为8~16位");
                }
                else if (!rul.test(inp)) {
                    $("#sp3").html("格式错误，必须包含字母、数字、特殊字符");
                }
                else {
                    $("#sp3").html("");
                    password1 = true;
                }
            });
            $("#in4").blur(function () {
                password2 = false;
                const fir = $("#in3").val();
                const sec = $(this).val();
                if (sec === "") {
                    $("#sp4").html("请重复输入密码");
                }
                else if (fir !== sec) {
                    $("#sp4").html("两次密码不一致");
                }
                else {
                    $("#sp4").html("");
                    password2 = true;
                }
            });
            $("#bu1").click(function () {
                if (!username || !email || !password1 || !password2) {
                    alert("注册失败");
                    return;
                }
                const usn = $("#in1").val();
                const eml = $("#in2").val();
                const pwd = $("#in3").val();
                for (let i = 0; i < localStorage.length; ++i) {
                    const kna = localStorage.key(i);
                    const str = localStorage.getItem(kna);
                    const site = JSON.parse(str);
                    if (usn === site.usn) {
                        alert("注册失败，用户名已存在");
                        return;
                    }
                    if (eml === site.eml) {
                        alert("注册失败，此邮箱已注册");
                        return;
                    }
                }
                const date = new Date();
                const obj = {usn: usn, eml: eml, pwd: pwd, fDate: date.toLocaleString(), lDate: null, rem: false};
                const str = JSON.stringify(obj);
                localStorage.setItem(eml, str);
                alert("注册成功");
                location.href = "https://yxl76.gitee.io/four_phase/login_success_page.html?=" + eml + "+" + pwd;
            });
        });
    </script>
</head>

<body>
<div class="container border border-primary" style="background-color: white; width: 450px; margin-top: 50px">
    <br>
    <div style="text-align: center;">
        <h1><strong>兰陈昕的第四期作业</strong></h1>
        <h2><strong>感受更精彩的世界</strong></h2>
        <p>已有账户<a href="https://yxl76.gitee.io/four_phase/login_page.html"> 点击登录</a></p>
    </div>
    <div style="width: 300px; margin: auto;">
        <form class="form-horizontal">
            <div class="form-group">
                <div class="input-group">
                <span class="Icon">
                    <i class="fa fa-user"></i>
                </span>
                    <input type="text" class="form-control" id="in1" placeholder="用户名">
                </div>
                <span id="sp1" style="color: red"></span>
            </div>
            <div class="form-group">
                <div class="input-group">
                <span class="Icon">
                    <i class="fa fa-envelope"></i>
                </span>
                    <input type="email" class="form-control" id="in2" placeholder="电子邮箱">
                </div>
                <span id="sp2" style="color: red"></span>
            </div>
            <div class="form-group">
                <div class="input-group">
                <span class="Icon">
                    <i class="fa fa-unlock"></i>
                </span>
                    <input type="password" class="form-control" id="in3" placeholder="密码">
                </div>
                <span id="sp3" style="color: red"></span>
            </div>
            <div class="form-group">
                <div class="input-group">
                <span class="Icon">
                    <i class="fa fa-lock"></i>
                </span>
                    <input type="password" class="form-control" id="in4" placeholder="请重复输入密码">
                </div>
                <span id="sp4" style="color: red"></span>
            </div>
            <div style="text-align: center;">
                <p>点击注册即表示您同意并愿意遵守</p>
                <p><a href="">用户协议</a>和<a href="">隐私政策</a>。</p>
            </div>
            <div style="width: 220px; margin: auto">
                <button type="button" class="btn btn-primary btn-block" id="bu1">注册</button>
            </div>
        </form>
    </div>
    <br>
</div>

</body>

</html>
